import { API } from '../../../config/api'

Page({
  data: {
    searchOrderNumber: '',
    selectedOrder: null
  },

  handleSearchInput(e) {
    this.setData({
      searchOrderNumber: e.detail.value
    })
  },

  searchOrder() {
    const { searchOrderNumber } = this.data
    if (!searchOrderNumber) {
      wx.showToast({
        title: '请输入订单编号',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '搜索中...'
    })

    // 调用订单搜索接口
    wx.request({
      url: `http://your-api-domain.com/api/orders/search`,
      method: 'GET',
      data: {
        orderNumber: searchOrderNumber
      },
      success: (res) => {
        if (res.data.success && res.data.data) {
          this.setData({
            selectedOrder: res.data.data
          })
        } else {
          wx.showToast({
            title: '未找到相关订单',
            icon: 'none'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '搜索失败，请重试',
          icon: 'none'
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  saveExpress() {
    if (!this.data.selectedOrder) {
      wx.showToast({
        title: '请先搜索订单',
        icon: 'none'
      })
      return
    }

    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    
    prevPage.setData({
      expressList: [...prevPage.data.expressList, this.data.selectedOrder]
    })

    wx.navigateBack()
  }
})